package org.dromara.northstar.gateway.okx;


import com.alibaba.fastjson.JSONObject;

import org.dromara.northstar.common.IDataSource;
import org.dromara.northstar.common.constant.ChannelType;
import org.dromara.northstar.common.model.Identifier;
import org.dromara.northstar.common.model.core.Contract;
import org.dromara.northstar.common.model.core.ContractDefinition;
import org.dromara.northstar.gateway.Instrument;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

import xyz.redtorch.pb.CoreEnum.CurrencyEnum;
import xyz.redtorch.pb.CoreEnum.ExchangeEnum;
import xyz.redtorch.pb.CoreEnum.ProductClassEnum;

public class OkxContract implements Instrument {

    private JSONObject json;

    private ContractDefinition contractDef;

    private IDataSource dataSrc;

    public OkxContract(JSONObject json, IDataSource dataSrc) {
        this.json = json;
        this.dataSrc = dataSrc;
    }

    @Override
    public String name() {
        return json.getString("instId");
    }

    @Override
    public Identifier identifier() {
        return Identifier.of(String.format("%s@%s@%s@%s", name(), exchange(), productClass(), channelType()));
    }

    @Override
    public ProductClassEnum productClass() {
        if (ProductClassEnum.SPOT.name().equals(json.getString("instType"))) {
            return ProductClassEnum.SPOT;
        } else {
            return ProductClassEnum.SWAP;
        }
    }

    @Override
    public ExchangeEnum exchange() {
        return ExchangeEnum.OKX;
    }

    @Override
    public ChannelType channelType() {
        return ChannelType.OKX;
    }

    @Override
    public void setContractDefinition(ContractDefinition contractDef) {
        this.contractDef = contractDef;
    }

    @Override
    public IDataSource dataSource() {
        return dataSrc;
    }

    /**
     * 该合约信息细节还待斟酌
     */
    @Override
    public Contract contract() {
        Instant e = Instant.ofEpochMilli(4133404802000L);
        LocalDate lastTradeDate = e.atZone(ZoneId.systemDefault()).toLocalDate();

        return Contract.builder()
                .gatewayId(ChannelType.OKX.toString())
                .symbol(name())
                .name(name())
                .fullName(name())
                .unifiedSymbol(String.format("%s@%s@%s", name(), exchange(), productClass()))
                .currency(CurrencyEnum.USD)
                .exchange(exchange())
                .productClass(productClass())
                .lastTradeDate(lastTradeDate)
                .priceTick(json.getDoubleValue("tickSz"))
                .pricePrecision(json.getString("tickSz").length() - json.getString("tickSz").indexOf('.') - 1)
                .quantityPrecision(json.getString("tickSz").length() - json.getString("tickSz").indexOf('.') - 1)
                .multiplier(json.getDoubleValue("minSz"))
                .contractId(identifier().value())
                .longMarginRatio(json.getDoubleValue("longMarginRatio"))
                .shortMarginRatio(json.getDoubleValue("shortMarginRatio"))
                .channelType(ChannelType.OKX)
                .tradable(true)
                .contractDefinition(contractDef)
                .build();
    }

}
